package org.example.controller;

import org.example.entity.Admin;
import org.example.entity.Certification;
import org.example.entity.JobPost;
import org.example.entity.User;
import org.example.service.UserService;
import org.example.service.JobPostService;
import org.example.service.CertificationService;
import org.example.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/admin")
@CrossOrigin(origins = "http://localhost:5173")
public class AdminController {

    private final UserService userService;
    private final JobPostService jobPostService;
    private final CertificationService certificationService;
    private final AdminService adminService;

    @Autowired
    public AdminController(UserService userService, JobPostService jobPostService, CertificationService certificationService, AdminService adminService) {
        this.userService = userService;
        this.jobPostService = jobPostService;
        this.certificationService = certificationService;
        this.adminService = adminService;
    }

    // 管理员登录
    @PostMapping("/login")
    public ResponseEntity<?> adminLogin(@RequestParam String username, @RequestParam String password) {
        Admin admin = adminService.login(username, password);
        if (admin != null) {
            return ResponseEntity.ok().body("登录成功");
        } else {
            return ResponseEntity.status(401).body("用户名或密码错误");
        }
    }

    // 管理员登出
    @GetMapping("/logout")
    public ResponseEntity<?> adminLogout() {
        adminService.logout();
        return ResponseEntity.ok().body("注销成功");
    }

    // 用户管理
    @GetMapping("/users")
    public List<User> getAllUsers() {
        return userService.findAllUsers();
    }

    @PutMapping("/users/{id}/status")
    public void updateUserStatus(@PathVariable Long id, @RequestParam boolean status) {
        userService.updateUserStatus(id, status);
    }

    // 岗位管理
    @GetMapping("/jobs")
    public List<JobPost> getAllJobs() {
        return jobPostService.findAllJobs();
    }

    @PutMapping("/jobs/{id}/status")
    public void updateJobStatus(@PathVariable Long id, @RequestParam String status) {
        jobPostService.updateJobPostStatus(id, status);
    }

    @DeleteMapping("/jobs/{id}")
    public void deleteJob(@PathVariable Long id) {
        jobPostService.deleteJobPost(id);
    }

    // 认证管理
    @GetMapping("/certifications")
    public List<Certification> getAllCertifications() {
        return certificationService.findAllCertifications();
    }

    @PutMapping("/certifications/{id}/approve")
    public void approveCertification(@PathVariable Long id) {
        certificationService.approveCertification(id);
    }

    @PutMapping("/certifications/{id}/reject")
    public void rejectCertification(@PathVariable Long id, @RequestParam String rejectReason) {
        certificationService.rejectCertification(id, rejectReason);
    }
}
